<template>
	<view class="wrap">
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录</view>
			<view style="border-bottom: #18B566 2px solid;margin-bottom: 100rpx;border-radius: 3rpx;"></view>
			<u-form :model="loginForm" ref="loginFormRef">
				<u-form-item prop="username">
					<view class="iconfont icon-icon_personal" style="margin:0rpx 20rpx;font-size: 24px;"></view>
					<u-input v-model="loginForm.username" placeholder="请输入用户名" />
				</u-form-item>
				<u-form-item prop="password" style="margin-top: 50rpx;">
					<view class="iconfont icon-icon_password1" style="margin:0rpx 20rpx;font-size: 24px;"></view>
					<u-input type="password" v-model="loginForm.password" placeholder="请输入密码" />
				</u-form-item>
				<u-gap height="80"></u-gap>
				<button @click="submitLogin" class="getSmsCode">
					<!-- 加载动画 -->
					<u-loading :show="loadingShow" style="margin-right: 10rpx;"></u-loading>
					登录
				</button>
			</u-form>
			
			<!-- 操作栏 -->
			<view class="alternative">
				<view class="password" @click="passwordLogin()">忘记密码</view>
				<view class="issue" @click="loginBy()">遇到问题</view>
			</view>
		</view>
	</view>
</template>

<script>
	//引入函数
	import { login } from '@/api/login';
	import { getUserInfo } from '@/api/login';
	export default {
		data() {
			return {
				erroryType: ['toast'],
				loadingShow: false,
				loginForm:{
					username: 'admin',
					password: '123',
				},
				// 表单校验规则
				rules: {
					username: [
						{ 
							required: true, 
							message: '请输入用户名!', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					password: [
						{
							required: true, 
							min: 2, 
							message: '密码不能少于5个字符', 
							trigger: ['change','blur'],
						}
					]
				}
			}
		},
		computed: {

		},
		onLoad() {

		},
		methods: {
			submitLogin() {
				this.loadingShow = true
				this.$refs.loginFormRef.validate(valid => {
					if (valid) {
						// 表单校验通过，发起请求
						login(this.loginForm).then(res => {
							this.loadingShow = false
							if (res.code == 20000) {
								// 保存 token
								uni.setStorageSync('auth-token', res.data.token);
								// 跳转至首页 (也可以跳转至上一页)
								this.$u.route({
									type: 'switchTab',
									url: '/pages/home/index'
								})
							} else {
								// 展示后台传入的错误信息
								uni.showToast({
									title: res.message,
									icon: 'none',
								})
							};
							/* 存储个人信息 */
							this.getLoginInfo()
						}).catch(err => {
							uni.showToast({
								title:'服务器异常，稍后重试！',
								icon: 'none'
							})
						})
					} else {
						// 表单校验失败
						uni.showToast({
							title: this.erroryType,
							icon: 'none',
						})
					}
				});
			},
			// 获取个人信息
			getLoginInfo(){
				getUserInfo().then( res => {
					uni.setStorage({
						key: 'user',
						data: JSON.stringify(res.data.loginUser),
					});
				})
			},
			passwordLogin(){
				this.$u.toast('开发中，敬请期待');
			},
			loginBy(type) {
				this.$u.toast('开发中，敬请期待');
			},
			goPage(url) {
				this.$u.route({
					url: url
				})
			},
			// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
			onReady() {
				this.$refs.loginFormRef.setRules(this.rules);
			}
		}
	};
</script>

<style lang="scss" scoped>
	.top{
		margin-top: 50rpx;
	}
	.wrap {
		font-size: 28rpx;

		.content {
			width: 600rpx;
			margin: 0 auto;
			padding-top: 80rpx;

			.title {
				text-align: left;
				font-size: 60rpx;
				font-weight: 500;
				margin-bottom: 40rpx;
			}

			input {
				text-align: left;
				margin-bottom: 10rpx;
				padding-bottom: 6rpx;
			}

			.tips {
				color: $u-type-info;
				font-size: 20rpx;
				margin-bottom: 60rpx;
				margin-top: 30rpx;
			}

			.getSmsCode {
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30rpx;
				padding: 12rpx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				margin-top: 30rpx;
				justify-content: space-between;
				margin-top: 30rpx;
			}
		}

		.bottom {
			.loginType {
				display: flex;
				padding: 260rpx 150rpx 150rpx 150rpx;
				justify-content: space-between;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28rpx;
				}
			}

			.hint {
				padding: 20rpx 40rpx;
				font-size: 20rpx;
				color: $u-tips-color;

				.link {
					color: $u-type-warning;
				}
			}
		}
	}
</style>